package net.pws.oos.web.stats;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SuppressWarnings({ "rawtypes", "unchecked" })
public class StatsComputer {

	private List<Map> users;

	private List<Map> tasks;

	private Map<String, UserStats> statses;

	public StatsComputer(List<Map> users, List<Map> tasks) {
		this.users = users;
		this.tasks = tasks;
	}

	public List<Map> computeStats() {
		initStatses();
		computeTask();
		joinUserMaps();
		return users;
	}

	private void initStatses() {
		statses = new HashMap<String, UserStats>(users.size());
		for (Map user : users) {
			String id = (String) user.get("id");
			statses.put(id, new UserStats());
		}
	}

	private void computeTask() {
		for (Map task : tasks) {
			String userId = (String) task.get("execPersonId");
			UserStats userStats = statses.get(userId);
			userStats.add(task);
		}
	}

	private void joinUserMaps() {
		for (Map user : users) {
			String id = (String) user.get("id");
			user.put("stats", statses.get(id).toStats());
		}
	}
}
